package com.jdd.modules.sdk.sdkcall;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jdd.common.api.vo.Result;
import com.jdd.common.constant.CacheConstant;
import com.jdd.common.constant.JddConstant;
import com.jdd.common.util.CacheUtil;
import com.jdd.common.util.LedControlUtils;
import com.jdd.common.util.SnowFlakeUtil;
import com.jdd.modules.parking.mapper.ParkCarEnterLogMapper;
import com.jdd.modules.parking.mapper.ParkCarOutLogMapper;
import com.jdd.modules.sdk.rs485Callback.ZhenShi485Callback;
import com.jdd.modules.sdk.utils.XiaoMingLedUtils;
import com.jdd.modules.parking.entity.ConfigParkInfo;
import com.jdd.modules.parking.entity.ParkArmInfo;
import com.jdd.modules.parking.mapper.ParkArmInfoMapper;
import com.jdd.modules.parking.service.IConfigParkInfoService;
import com.jdd.modules.parking.service.IParkArmInfoService;
import com.jdd.modules.sdk.SdkCallInterface;
import com.jdd.modules.sdk.config.CameraSdk;
import com.jdd.modules.sdk.sdkidentifycallbackresults.ZhenShiSdkIdentifyCallbackResults;
import com.jdd.modules.sdk.utils.*;
import com.jdd.modules.sdkopengatelog.entity.SdkOpenGateLog;
import com.jdd.modules.sdkopengatelog.service.ISdkOpenGateLogService;
import com.sun.jna.Pointer;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.Date;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * @version 1.0
 * @Description:
 * @author： jdd尚岳
 * @Date: 2021-07-29 17:32
 */
@Slf4j
@Component
@Primary
public class ZhenShiSdkCallInterface extends SdkCallInterface {
    @Resource
    private CameraSdk cameraSdk;
    @Resource
    private CacheUtil cacheUtill;
    @Resource
    private ParkArmInfoMapper parkArmInfoMapper;
    @Resource
    private ZhenShiSdkIdentifyCallbackResults zhenShiSdkIdentifyCallbackResults;
    @Resource
    private IParkArmInfoService parkArmInfoService;
    @Resource
    private ParkCarEnterLogMapper parkCarEnterLogMapper;
    @Resource
    private ParkCarOutLogMapper parkCarOutLogMapper;
    @Resource
    private ISdkOpenGateLogService sdkOpenGateLogService;
    @Resource
    private IConfigParkInfoService configParkInfoService;
    @Resource
    private ZhenShi485Callback zhenShi485Callback;
    VZLPRC_GPIO_RECV_CALLBACK recvCallback = new VZLPRC_GPIO_RECV_CALLBACK();
    @Override
    public Result SdkUnfold(String serialNo) {
        try {
            if (StringUtils.isBlank(serialNo)) {
                return Result.error("设备序列号不能为空!");
            }
            String handle = cacheUtill.get(CacheConstant.SDK_HANDLE, serialNo);
            if (StringUtils.isBlank(handle)) {
                return Result.error("请对设备进行初始化");
            }
            int i = cameraSdk.zhenshiSdk.VzLPRClient_SetIOOutputAuto(Long.parseLong(handle), 0, 500);
            LambdaQueryWrapper<ParkArmInfo> queryWrapper = new LambdaQueryWrapper();
            queryWrapper.eq(ParkArmInfo::getSerialNo, serialNo);
            ParkArmInfo parkArmInfo = parkArmInfoMapper.selectOne(queryWrapper);
            SdkOpenGateLog openGateLog = new SdkOpenGateLog();
            BeanUtils.copyProperties(parkArmInfo, openGateLog);
            openGateLog.setId(SnowFlakeUtil.getId());
            openGateLog.setCreateTime(new Date());
            openGateLog.setStateCode(i);

            sdkOpenGateLogService.save(openGateLog);
            if (i != 0) {
                return Result.error("开闸失败");
            }
        } catch (Exception e) {
            log.error("sdk工具类:通过io进行开闸->{}", e);
        }
        return Result.ok().success("开闸成功");
    }


    @Override
    public Result SdkUnfold(String swichType, String serialNo, String plateNo) {
        int i = -1;
        if (StringUtils.isBlank(serialNo)) {
            return Result.error("设备序列号不能为空!");
        }
        String handle = cacheUtill.get(CacheConstant.SDK_HANDLE, serialNo);
        if (StringUtils.isBlank(handle)) {
            return Result.error("请对设备进行初始化");
        }

        try {
            i = cameraSdk.zhenshiSdk.VzLPRClient_SetIOOutputAuto(Long.parseLong(handle), 0, 500);
        } catch (Exception e) {
            log.error("sdk工具类:通过io进行开闸->{}", e);
        }
        LambdaQueryWrapper<ParkArmInfo> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(ParkArmInfo::getSerialNo, serialNo);
        ParkArmInfo parkArmInfo = parkArmInfoMapper.selectOne(queryWrapper);
        //出入口id
        String carId;
        //0进口，1出口
        if(JddConstant.StringNumber.STRING_ZERO.equals(parkArmInfo.getArmType())){
            carId = parkCarEnterLogMapper.findIdBySerialAndPlate(serialNo, plateNo);
        }else{
            carId = parkCarOutLogMapper.findIdBySerialAndPlate(serialNo, plateNo);
        }
        SdkOpenGateLog openGateLog = new SdkOpenGateLog();
        BeanUtils.copyProperties(parkArmInfo, openGateLog);
        openGateLog.setId(SnowFlakeUtil.getId());
        openGateLog.setCreateTime(new Date());
        openGateLog.setPlateNo(plateNo);
        openGateLog.setStateCode(i);
        openGateLog.setSwichType(swichType);
        openGateLog.setCapPlace(carId);
        sdkOpenGateLogService.save(openGateLog);

        if (i != 0) {
            return Result.error("开闸失败");
        }

        return Result.ok().success("开闸成功");
    }

    @Override
    public Result CameraSdkInit(String ip) {

        if (org.apache.commons.lang3.StringUtils.isBlank(ip)) {
            return Result.error("设备IP不能为空!");
        }
        //如果先前已经初始化相机，就不再初始化
        ParkArmInfo parkArmInfoByIp = parkArmInfoService.findParkArmInfoByIp(ip);
        if(null==parkArmInfoByIp){
            return Result.error("查找不到设备信息");
        }
        try {
            String serialNo = parkArmInfoByIp.getSerialNo();
            log.info("========设备开始进行初始化========：ip->{}", ip);
             //根据设备IP,端口,相机默认账号和密码获取句柄
            long handle = cameraSdk.zhenshiSdk.VzLPRClient_Open(ip, 80, "admin", "admin");
            log.info("========获取设备句柄========：handle->{}", handle);
            if (handle == 0) {
                log.error("设备初始化失败: ip->{}", ip);
                return Result.error(ip + ":设备初始化失败");
            }
            cacheUtill.put(CacheConstant.GET_SDK_SERIALNO, String.valueOf(handle), serialNo);
            cacheUtill.put(CacheConstant.SDK_HANDLE, serialNo, handle);
            //获取透明通道的句柄
            int serial_handle = cameraSdk.zhenshiSdk.VzLPRClient_SerialStart(handle, 0, zhenShi485Callback, Pointer.NULL);
            if (serial_handle == 0) {
                log.error("485回调结果初始化失败: ip->{}", ip);
                return Result.error(ip + ":485回调结果初始化失败");
            }

            cacheUtill.put(CacheConstant.GET_SERIAL_HANDLE_SERIALNO, String.valueOf(serial_handle), serialNo);

            cacheUtill.put(CacheConstant.SERIAL_HANDLE, serialNo, serial_handle);

             cameraSdk.zhenshiSdk.VzLPRClient_SetGPIORecvCallBack(handle,recvCallback, Pointer.NULL);

            int callbackindex = cameraSdk.zhenshiSdk.VzLPRClient_SetPlateInfoCallBack(handle, zhenShiSdkIdentifyCallbackResults, Pointer.NULL, 1);
            if (callbackindex != 0) {
                log.error("回调结果初始化失败: ip->{}", ip);
                return Result.error(ip + ":回调结果初始化失败");
            }
        } catch (Exception e) {
            log.error("sdk工具类:设备初始化失败   —>{}", e);
        }
        log.info("设备初始化成功:ip->{}", ip);
        return Result.ok("初始化成功");
    }



    @Override
    public Result Sdk485DaoZhaUploadMessage(String serialNo) {

        if (StringUtils.isBlank(serialNo)) {
            return Result.error("设备序列号不能为空!");
        }
        String handle = cacheUtill.get(CacheConstant.SERIAL_HANDLE, serialNo);
        if (StringUtils.isBlank(handle)) {
            return Result.error("请对设备进行初始化");
        }
        try {
            //开启透明通道
            int i = Integer.parseInt(handle);
            byte[] data = null;
              data = DaoZhaUtils.setDaoZhaUploadLocation();
             int ret = cameraSdk.zhenshiSdk.VzLPRClient_SerialSend(i, data, data.length);
            if (ret != 0) {
                return Result.error("播放失败!");
            }
        } catch (Exception e) {
            log.error("sdk工具类:LED屏幕发送语音播报方法—>{}", e);
        }
        return Result.ok().success("成功");
    }



    @Override
    public Result Sdk485VoiceCommunication(String serialNo, String str) {

        if (StringUtils.isBlank(serialNo)) {
            return Result.error("设备序列号不能为空!");
        }
        String handle = cacheUtill.get(CacheConstant.SERIAL_HANDLE, serialNo);
        if (StringUtils.isBlank(handle)) {
            return Result.error("请对设备进行初始化");
        }
        try {
            //开启透明通道
            int i = Integer.parseInt(handle);
            byte[] data = null;
            ConfigParkInfo configParkInfo = getConfigParkInfo();
            if (JddConstant.CameraControl.CONTROLPANEL_FANGKONG.equals(configParkInfo.getControlPanelType())) {
                data = LedControlUtils.ledPlayVoice(str.trim());
            }
            if (JddConstant.CameraControl.CONTROLPANEL_FEILIDE.equals(configParkInfo.getControlPanelType())) {
                data = FeiLiDeLedUtils.sdKUnkledSpeech(str.trim());
            }
            if (JddConstant.CameraControl.CONTROLPANEL_QIANYI.equals(configParkInfo.getControlPanelType())) {
                data = QianYiLedUtils.sdKUnkledSpeech(str.trim());
            }
            if (JddConstant.CameraControl.CONTROLPANEL_XIAOMING.equals(configParkInfo.getControlPanelType())) {
                data = XiaoMingLedUtils.ledPlayVoice(str.trim());
            }
            if(JddConstant.CameraControl.CONTROLPANEL_QIXIE.equals(configParkInfo.getControlPanelType())){
                data= QiXieLedUtils.sdKUnkledSpeech(str.trim());
            }
            if(JddConstant.CameraControl.CONTROLPANEL_SMALL_FANGKONG.equals(configParkInfo.getControlPanelType())){
                data= SmallFangKongLedUtils.ledPlayVoice(str.trim());
            }
            if(JddConstant.CameraControl.CONTROLPANEL_YANGZHONGLAI_GREEN.equals(configParkInfo.getControlPanelType())){
                data= YangZhongLaiGreenLedUtils.sdKUnkledSpeech(str.trim());
            }
            if(JddConstant.CameraControl.CONTROLPANEL_ANKUAI.equals(configParkInfo.getControlPanelType())){
                data= AnKuaiLedUtils.sdKUnkledSpeech(str.trim());
            }
            int ret = cameraSdk.zhenshiSdk.VzLPRClient_SerialSend(i, data, data.length);
            if (ret != 0) {
                return Result.error("播放失败!");
            }
        } catch (Exception e) {
            log.error("sdk工具类:LED屏幕发送语音播报方法—>{}", e);
        }
        return Result.ok().success("成功");
    }

    @Override
    public Result Sdk485TestContentCommunication(String serialNo, String str, String like) {
        String handle = cacheUtill.get(CacheConstant.SERIAL_HANDLE, serialNo);
        if (StringUtils.isBlank(handle)) {
            return Result.error("请对设备进行初始化");
        }
        //开启透明通道
        int i = Integer.parseInt(handle);
        try {
            byte[] data = null;
            ConfigParkInfo configParkInfo = getConfigParkInfo();
            if (JddConstant.CameraControl.CONTROLPANEL_FANGKONG.equals(configParkInfo.getControlPanelType())) {
                data = LedControlUtils.ledScreenShowAgreement(str.trim(), like);
            }
            if (JddConstant.CameraControl.CONTROLPANEL_FEILIDE.equals(configParkInfo.getControlPanelType())) {
                data = FeiLiDeLedUtils.ledTemporaryDisplay(str.trim(), like);
            }
            if (JddConstant.CameraControl.CONTROLPANEL_QIANYI.equals(configParkInfo.getControlPanelType())) {
                data = QianYiLedUtils.ledTemporaryDisplay(str.trim(), like);
            }
            if (JddConstant.CameraControl.CONTROLPANEL_XIAOMING.equals(configParkInfo.getControlPanelType())) {
                data = XiaoMingLedUtils.ledTemporaryDisplay(str.trim(), like);
            }
            if(JddConstant.CameraControl.CONTROLPANEL_QIXIE.equals(configParkInfo.getControlPanelType())){
                data= QiXieLedUtils.ledTemporaryDisplay(str.trim(),like);
            }
            if(JddConstant.CameraControl.CONTROLPANEL_SMALL_FANGKONG.equals(configParkInfo.getControlPanelType())){
                data= SmallFangKongLedUtils.ledScreenShowAgreement(str.trim(),like);
            }
            if(JddConstant.CameraControl.CONTROLPANEL_YANGZHONGLAI_GREEN.equals(configParkInfo.getControlPanelType())){
                data= YangZhongLaiGreenLedUtils.ledTemporaryDisplay(str.trim(),like);
            }
            if(JddConstant.CameraControl.CONTROLPANEL_ANKUAI.equals(configParkInfo.getControlPanelType())){
                data= AnKuaiLedUtils.ledTemporaryDisplay(str.trim(),like);
            }
            int ret = cameraSdk.zhenshiSdk.VzLPRClient_SerialSend(i, data, data.length);
            if (ret != 0) {
                return Result.error("发送失败!");
            }
        } catch (Exception e) {
            log.error("sdk工具类:LED屏幕发送文字临时播放信息方法—>{}", e);
        }
        return Result.ok().success("成功");
    }
    /**
     *  空闲状态指令
     *  方控：发送广告指令
     *  菲利德：进行内容存储
     * @param str
     * @return
     * @author renshangyi
     * @date 2022/1/26
     */
    @Override
    public Result Sdk485LedAdvertising(String serialNo, String str, String like) {
        String handle = cacheUtill.get(CacheConstant.SERIAL_HANDLE, serialNo);
        if (StringUtils.isBlank(handle)) {
            return Result.error("请对设备进行初始化");
        }
        //开启透明通道
        int i = Integer.parseInt(handle);
        try {
            byte[] data = null;
            ConfigParkInfo configParkInfo = getConfigParkInfo();
            if (JddConstant.CameraControl.CONTROLPANEL_FANGKONG.equals(configParkInfo.getControlPanelType())) {
                data = LedControlUtils.ledScreenShowPermanent(str.trim(), like);
            }
            if (JddConstant.CameraControl.CONTROLPANEL_FEILIDE.equals(configParkInfo.getControlPanelType())) {
                data = FeiLiDeLedUtils.ledDownloadText(str.trim(), like);
            }
            if (JddConstant.CameraControl.CONTROLPANEL_SMALL_FANGKONG.equals(configParkInfo.getControlPanelType())) {
                data = LedControlUtils.ledScreenShowPermanent(str.trim(), like);
            }
            int ret = cameraSdk.zhenshiSdk.VzLPRClient_SerialSend(i, data, data.length);
            if (ret != 0) {
                return Result.error("发送失败!");
            }
        } catch (Exception e) {
            log.error("sdk工具类:LED屏幕发送文字临时播放信息方法—>{}", e);
        }
        return Result.ok().success("成功");
    }

    @Override
    public Result Sdk485LedGreenToRed(String serialNo, String like) {
        String handle = cacheUtill.get(CacheConstant.SERIAL_HANDLE, serialNo);
        if (StringUtils.isBlank(handle)) {
            return Result.error("请对设备进行初始化");
        }
        //开启透明通道
        int i = Integer.parseInt(handle);
        try {
            byte[] data = null;
            ConfigParkInfo configParkInfo = getConfigParkInfo();
            if (JddConstant.CameraControl.CONTROLPANEL_SMALL_FANGKONG.equals(configParkInfo.getControlPanelType())) {
                data = SmallFangKongLedUtils.ledGreenToRed(like);
            }
            int ret = cameraSdk.zhenshiSdk.VzLPRClient_SerialSend(i, data, data.length);
            if (ret != 0) {
                return Result.error("发送失败!");
            }
        } catch (Exception e) {
            log.error("sdk工具类:LED屏幕发送文字临时播放信息方法—>{}", e);
        }
        return Result.ok().success("成功");
    }

    /**
     *  方控：修改日期显示位置
     * @param   serialNo
     * @return
     * @author renshangyi
     * @date 2022/1/26
     */
    @Override
    public Result Sdk485LedModifyDatePosition(String serialNo,String order) {
        String handle = cacheUtill.get(CacheConstant.SERIAL_HANDLE, serialNo);
        if (StringUtils.isBlank(handle)) {
            return Result.error("请对设备进行初始化");
        }
        //开启透明通道
        int i = Integer.parseInt(handle);
        try {
            byte[] data = null;
            ConfigParkInfo configParkInfo = getConfigParkInfo();
            if (JddConstant.CameraControl.CONTROLPANEL_FANGKONG.equals(configParkInfo.getControlPanelType())) {
                data = LedControlUtils.hexToByteArray(order);
            }else{
                log.info("------当前系统控制板不是方控,不能修改日期位置----");
                return Result.error("无法修改");
            }
            int ret = cameraSdk.zhenshiSdk.VzLPRClient_SerialSend(i, data, data.length);
            if (ret != 0) {
                return Result.error("发送失败!");
            }
        } catch (Exception e) {
            log.error("sdk工具类:LED屏幕发送文字临时播放信息方法—>{}", e);
        }
        return Result.ok().success("成功");
    }

    @Override
    public Result GetIsConnect(String serialNo) {
        try {
            if (StringUtils.isBlank(serialNo)) {
                return Result.error("设备序列号不能为空!");
            }
            String handle = cacheUtill.get(CacheConstant.SDK_HANDLE, serialNo);
            if (StringUtils.isBlank(handle)) {
                return Result.error("请对设备进行初始化");
            }
            //state 输入获取状态的变量地址，输出内容为 1已连上，0未连上
            byte[] state = new byte[1];
            int ret = cameraSdk.zhenshiSdk.VzLPRClient_IsConnected(Long.parseLong(handle), state);
            if (ret != 0) {
                return Result.error("设备处于断线状态!");
            }
            if (byteArray2Int(state) == 0) {
                return Result.error("设备处于断线状态!");
            }
        } catch (Exception e) {
            log.error("sdk工具类:查询设备在线接口->{}", e);
        }
        return Result.ok().success("连接成功");
    }

    @Override
    public Result TriggerExt(String serialNo) {
        try {
            if (org.apache.commons.lang3.StringUtils.isBlank(serialNo)) {
                return Result.error("设备序列号不能为空!");
            }
            String handle = cacheUtill.get(CacheConstant.SDK_HANDLE, serialNo);
            if (org.apache.commons.lang3.StringUtils.isBlank(handle)) {
                return Result.error("请对设备进行初始化");
            }
            int callbackindex = cameraSdk.getZhenshiSdk().VzLPRClient_ForceTrigger(Long.parseLong(handle));
            if (callbackindex != 0) {
                return Result.error("重新识别失败");
            }
        } catch (Exception e) {
            log.error("sdk工具类:软触发接口->{}", e);
        }
        return Result.ok().success("重新识别成功");
    }
    /*** 功能描述: 同步菲力德的时间
     * @param serialNo 序列号
     * @Author: lcy
     * @Date: 2022/1/4
     */
    public Result Sdk485Calibration(String serialNo){
        if (StringUtils.isBlank(serialNo)) {
            return Result.error("设备序列号不能为空!");
        }
        String handle = cacheUtill.get(CacheConstant.SERIAL_HANDLE, serialNo);
        if (StringUtils.isBlank(handle)) {
            return Result.error("请对设备进行初始化");
        }
        try {
            //开启透明通道
            int i = Integer.parseInt(handle);
            byte[] data = null;
            ConfigParkInfo configParkInfo = getConfigParkInfo();
            if (JddConstant.CameraControl.CONTROLPANEL_FEILIDE.equals(configParkInfo.getControlPanelType())) {
                data = FeiLiDeLedUtils.calibrationTime();
            }else{
                log.info("------当前系统控制板不是菲力德,无需同步----");
                return Result.error("无需同步");
            }
            int ret = cameraSdk.zhenshiSdk.VzLPRClient_SerialSend(i, data, data.length);
            if (ret != 0) {
                return Result.error("同步失败");
            }
        } catch (Exception e) {
            log.error("sdk工具类:同步时间—>{}", e);
        }
        return Result.ok().success("成功");
    }

    /**
     * 取消临显内容
     * @param serialNo
     * @return
     * @author renshangyi
     * @date 2022/1/15
     */
    @Override
    public Result Sdk485LedNoContentCommunication(String serialNo){
        if (StringUtils.isBlank(serialNo)) {
            return Result.error("设备序列号不能为空!");
        }
        String handle = cacheUtill.get(CacheConstant.SERIAL_HANDLE, serialNo);
        if (StringUtils.isBlank(handle)) {
            return Result.error("请对设备进行初始化");
        }
        try {
            //开启透明通道
            int i = Integer.parseInt(handle);
            byte[] data = null;
            ConfigParkInfo configParkInfo = getConfigParkInfo();
            if (JddConstant.CameraControl.CAMERA_ZHENSHI.equals(configParkInfo.getControlPanelType())) {
                data = LedControlUtils.hexToByteArray("AA553864002100010FA409AF");
            }else{
                log.info("------当前系统控制板不是方控,无法取消临显内容----");
                return Result.error("无法取消临显内容");
            }
            int ret = cameraSdk.zhenshiSdk.VzLPRClient_SerialSend(i, data, data.length);
            if (ret != 0) {
                return Result.error("无法取消临显内容");
            }
        } catch (Exception e) {
            log.error("sdk工具类:同步时间—>{}", e);
        }
        return Result.ok().success("成功");
    }

    /**
     * 相机地感回调
     */
    public class VZLPRC_GPIO_RECV_CALLBACK implements ZHENSHISDK.VZLPRC_GPIO_RECV_CALLBACK {
        @Override
        public void invoke(long handle, int nGPIOId, int nVal, Pointer pUserData) {
         log.info("===========================获取地感状态======================================");
            String serialNo = cacheUtill.get(CacheConstant.GET_SDK_SERIALNO, String.valueOf(handle));
             if (org.apache.commons.lang3.StringUtils.isBlank(serialNo)){
                  log.info("设备句柄失效");
                  return;
             }
            cacheUtill.put(CacheConstant.GET_SDK_DOCK_CAR,serialNo,String.valueOf(nVal));


        }
    }
    public void setTime(String serialNo){
        String handle = cacheUtill.get(CacheConstant.SERIAL_HANDLE, serialNo);
        if (StringUtils.isBlank(handle)) {
            log.error("没找到");
        }
        //开启透明通道
        int i = Integer.parseInt(handle);
        try {
            byte[] data = FeiLiDeLedUtils.calibrationTime();
            int ret = cameraSdk.zhenshiSdk.VzLPRClient_SerialSend(i, data, data.length);
            if (ret != 0) {
                log.error("失败");
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
    /**
     * @author lcy
     * @date 2021/8/13
     * @Description 拿到当前相机
     */
    private ConfigParkInfo getConfigParkInfo() {
        //这里不作null判断，调用的方法做判断
        return configParkInfoService.findConfigParkInfoByParkCode();
    }

    /**
     * @param b
     * @return
     * @Description byte数组转换为整型
     * @author shangyue
     * @Date 2021/4/6
     */
    public int byteArray2Int(byte[] b) {
        int value = 0;
        for (int i = b.length - 1; i >= 0; i--) {
            int shift = (b.length - 1 - i) * 8;
            value += (b[i] & 0x000000FF) << shift;
        }

        return value;
    }

}
